package knowse.app.daos;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class CommitDAO {
	// If error message is needed
	private final String TAG = "CommitDAO";
	private final String ERRORMSG = "Insertion failed";
	// Table name
	protected static final String Table = "LogCommit";
	// Primary key
	protected static final String CommitID = "CommitID";
	// Other attributes
	protected static final String json_class = "json_class";
	protected static final String AuthorName = "AuthorName";
	protected static final String StartTime = "StartTime";
	protected static final String StartDate = "StartDate";
	protected static final String CommitHash = "CommitHash";
	protected static final String ShortCommitHash = "ShortCommitHash";
	protected static final String TimeStamp="TimeStamp";	
	protected static final String NumChanges="NumChanges";
	// Foreign key
	protected static final String LogID = "LogID";
	
	/*
	 * sqlStr= select json_class, AuthorName, StartTime, StartDate, CommitHash, ShortCommitHash,
	 *  TimeStamp from LogCommit;		
	 */
	protected static final String[] Columns={json_class, AuthorName, StartTime, StartDate, CommitHash, ShortCommitHash, TimeStamp ,NumChanges, CommitID};
	
	public CommitDAO() {
		
	}
	
	public void store(CommitDataObject cdo, Integer logID) throws Exception{
		ContentValues values= new ContentValues();
		values.put(json_class, cdo.getJsonClass());
		values.put(AuthorName, cdo.getAuthorName());
		values.put(StartTime, cdo.getStartTime());
		values.put(StartDate, cdo.getStartDate());
		values.put(CommitHash, cdo.getCommitHash());
		values.put(ShortCommitHash, cdo.getShortCommitHash());
		values.put(TimeStamp, cdo.getTimeStamp().getTime());
		values.put(NumChanges, cdo.getNumChanges());
		
		values.put(LogID, logID);
		
		DBHelper dbHelper = new DBHelper();
		
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		
		long insertResult = db.insert(Table, null, values);
		if(insertResult < 0){
			throw new Exception(TAG + " " + ERRORMSG);
		}
		db.close();
	}
	
	public void storeTrans(ArrayList<CommitDataObject> list) throws Exception{
		DBHelper dbHelper = new DBHelper();
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.beginTransaction();
		for(CommitDataObject cdo : list){
			ContentValues values= new ContentValues();
			values.put(json_class, cdo.getJsonClass());
			values.put(AuthorName, cdo.getAuthorName());
			values.put(StartTime, cdo.getStartTime());
			values.put(StartDate, cdo.getStartDate());
			values.put(CommitHash, cdo.getCommitHash());
			values.put(ShortCommitHash, cdo.getShortCommitHash());
			values.put(TimeStamp, cdo.getTimeStamp().getTime());
			values.put(NumChanges, cdo.getNumChanges());
			
			values.put(LogID, cdo.getLogID());
			
			Long insertResult = db.insert(Table, null, values);
			if(insertResult < 0){
				throw new Exception(TAG + " " + ERRORMSG);
			}
		}
		db.setTransactionSuccessful();
		db.endTransaction();
		db.close();
	}

	public ArrayList<CommitDataObject> fetchCommits(String limit, Boolean isTimeDescending) {
		
		ArrayList<CommitDataObject> commitArr = new ArrayList<CommitDataObject>();
		// TODO Auto-generated method stub
		/*
		 * 1. DBHelper and SqlBase
		 * 3. Curser
		 * 4. Parsing to CommitObject
		 * 5. Add to ArrayList
		 */
		
		SQLiteDatabase db = new DBHelper().getReadableDatabase();
		
		String isDesc="";
		if(isTimeDescending)
			isDesc=" desc";
		
		Cursor cursor=db.query(Table, Columns, null, null, null, null, TimeStamp+isDesc, limit);
		while(cursor.moveToNext()){
			
			//parsing and create CommitDataObject
			int commitID=cursor.getInt(cursor.getColumnIndex(CommitID));
			String jsonClass=cursor.getString(cursor.getColumnIndex(json_class));
			String authorname=cursor.getString(cursor.getColumnIndex(AuthorName));
			String starttime=cursor.getString(cursor.getColumnIndex(StartTime));
			String startdate=cursor.getString(cursor.getColumnIndex(StartDate));
			String commitHash=cursor.getString(cursor.getColumnIndex(CommitHash));
			String shortCommitHash=cursor.getString(cursor.getColumnIndex(ShortCommitHash));
			int numChanges=cursor.getInt(cursor.getColumnIndex(NumChanges));
			CommitDataObject cdo=new CommitDataObject(jsonClass, startdate, starttime, commitHash, authorname,shortCommitHash, commitID, numChanges);
			
			
			Log.e(TAG, starttime+" "+startdate);
		}
		
		
		
		
		
		return commitArr;
	}

}